from gameServer import gameServer
from vec2 import Vec2
from collectCube import CollectCube
from bullet import Bullet
from soundServer import sound

class Gun(CollectCube):
    
    def __init__(self):
        CollectCube.__init__(self)
        
        self.setVar("texture", "gun")
        
        self.setVar("firingRate", 0.35)
        self.fireCount = 0
        self.setVar("timedItem", True)
        self.setVar("lifeSpan", 7.5)
        self.setVar("lifeBlinkTime", 1)
        
    def update(self, cmd, args):
        CollectCube.update(self, cmd, args)
        
        dt = args["dtime"]
        if(self.fireCount > -10):
            self.fireCount -= dt
        
    def use(self, cmd, args):
        p = self.getVar("itemPlayer")
         
        if(self.fireCount < 0):
            self.fireCount = self.getVar("firingRate")
            
            if(not(p == None)):
                player = p.getVar("player")
                playerP = p.getVar("pos")
                face = p.getVar("face")
                
                b = gameServer.addObject(gameServer.makeObjectName("Bullet_"), Bullet(player, playerP, face))
                sound.play("gun_fire")